package eclipse.lib.sockets;
import eclipse.server.*;
import eclipse.server.data.*;
import eclipse.lib.scripting.*;
/**
 * The ServerDataHandle class handles all TCP messages sent to the Server
 */
public class ServerDataHandle
{
	private static final char msg_REQUESTMAX 		= 0;
	private static final char msg_REQUESTNEWACCOUNT	= 1;
	private static final char msg_SENDCHARPOS		= 2;
	private static final char msg_REQUESTLOGIN		= 3;
	/** Default sent "error 404, file does not exist" message */
	public static String DEFAULT404 = "" +
		"<html>" +
			"<body>" +
				"Page not found!" +
			"</body>" +
		"</html>";			
	/**
	 * Processes a message sent to the Server
	 * @param index Socket that the message came from
	 * @param netMsg Message recieved
	 */
	public static void process(int index, String netMsg)
	{	
		if(netMsg.length() == 0)
			return;				
		else if(netMsg.charAt(0) == msg_REQUESTMAX)
			sendMax(index);
		else if(netMsg.charAt(0) == msg_REQUESTNEWACCOUNT)
			sendNewAccountConf(index, netMsg);
		else if(netMsg.charAt(0) == msg_SENDCHARPOS)
			sendCharPosToAll(index, netMsg);
		else if(netMsg.charAt(0) == msg_REQUESTLOGIN)
			sendLoginConf(index, netMsg);
		else 
			unknownRequest(index, netMsg);
			
	}
	public static void unknownRequest(int index, String netMsg)
	{
		JScript.evaluate("unknownRequest", index, netMsg);
	}	
	/**
	 * Sends the character position to all players on the same map.
	 * @param index Client where the message came from
	 * @param netMsg Message recieved
	 */
	public static void sendCharPosToAll(int index, String netMsg)
	{		
		//TODO: send only to same map
		String msg = msg_SENDCHARPOS + "";
		int dir 	= ByteInterpreter.getIntFromString(netMsg, 1);
		int frame	= ByteInterpreter.getIntFromString(netMsg, 3);
		float xPos 	= ByteInterpreter.getFloatFromString(netMsg, 5);
		float yPos 	= ByteInterpreter.getFloatFromString(netMsg, 7);
		float zPos 	= ByteInterpreter.getFloatFromString(netMsg, 9);
		msg = msg + ByteInterpreter.appendIntegerToString(index);
		msg = msg + ByteInterpreter.appendIntegerToString(dir);
		msg = msg + ByteInterpreter.appendIntegerToString(frame);
		msg = msg + ByteInterpreter.appendFloatToString(xPos);
		msg = msg + ByteInterpreter.appendFloatToString(yPos);
		msg = msg + ByteInterpreter.appendFloatToString(zPos);
		msg = msg + "\n";
		sendMsgToMapBut(msg, Database.player[index].character.cMap, index);				
	}
	/** Send the maximum data to the client */
	public static void sendMax(int index)
	{
		String msgCode = msg_REQUESTMAX + "";
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXPLAYERS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXITEMS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXMAPS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXNPCS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXSHOPS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXSPELLS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXCLASSES);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(index);
		Database.client[index].sendMsg(msgCode + "\n");
	}
	/** Sends the confirmation that the account was created */
	public static void sendNewAccountConf(int index, String netMsg)
	{
		String split 	= eclipse.Constants.SEP_CHAR + "";		
		String msgCode = msg_REQUESTNEWACCOUNT + "";
		String parse[] 	= netMsg.split(split);	
		
		try
		{
			String uName 	= parse[0].replace(msg_REQUESTNEWACCOUNT + "", "");		
			String pWord	= parse[1];
			Account nAcc = new Account();
			if(nAcc.makeAccount(uName, pWord))
				msgCode = msgCode + "t";
			else
				msgCode = msgCode + "f";
		}
		catch(Exception e)
		{
			msgCode = msgCode + "f";
		}		
		Database.client[index].sendMsg(msgCode + "\n");
	}
	/**
	 *  Sends a message to everyone but the specified player.
	 *  @param msg Message to send
	 *  @param exclude Player not to recieve the message
	 */ 
	public static void sendMsgToAllBut(String msg, int exclude)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(i != exclude)
			{
				if(Database.client[i] != null)			
				{
					Database.client[i].sendMsg(msg);
				}
			}
		}
	}
	/**
	 * Sends a message to everyone on a single map
	 * @param msg Message to be sent
	 * @param map Map to recieve the message
	 */
	public static void sendMsgToMap(String msg, int map)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(Database.client[i] != null)
			{
				if(Database.player[i].character.cMap == map)
				{
					Database.client[i].sendMsg(msg);
				}
			}
		}
	}
	/**
	 * Sends a message to the map excluding a specific player
	 * @param msg Message to be sent
	 * @param map Map to recieve the messag
	 * @param exclude Person not to recieve the message
	 */
	public static void sendMsgToMapBut(String msg, int map, int exclude)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(Database.client[i] != null)
			{
				if(i != exclude)
				{
					if(Database.player[i].character.cMap == map)
					{
						Database.client[i].sendMsg(msg);
					}
				}
			}
		}
	}
	/**
	 * Sends a message to everyone connected.
	 * @param msg Message to be sent
	 */
	public static void sendMsgToAll(String msg)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(Database.client[i] != null)
				Database.client[i].sendMsg(msg);
		}		
	}
	public static void sendLoginConf(int index, String netMsg)
	{
		String split	= eclipse.Constants.SEP_CHAR + "";
		String msgCode	= msg_REQUESTLOGIN + "";
		String parse[]	= netMsg.split(split);
		
		try
		{
			String uName	= parse[0].replace(msg_REQUESTLOGIN + "", "");
			String pWord	= parse[1];
			Account pAcc	= new Account();
			if(pAcc.loadAccount(index, uName, pWord))
				msgCode = msgCode + "t";
			else
				msgCode = msgCode + "f";
		}
		catch(Exception e)
		{
			msgCode = msgCode + "f";
		}
		Database.client[index].sendMsg(msgCode + "\n");
		JScript.evaluate("onLogin", index);
	}	
}
